
using System;
using System.Data;

namespace Serpis
{
	public class AppDbConnection
	{
		//Singleton
		private static AppDbConnection instance = new AppDbConnection();
		private string parameterPrefix;

		private AppDbConnection()
		{
		}
		
		public static AppDbConnection Instance {
			get {return instance;}
		}
		
		private string connectionString;
		private Type dbConnectionType;
		private IDbConnection dbConnection;
		
		public string ConnectionString {
			get {return connectionString;}
			set {connectionString = value;}
		}
		
		public string ParameterPrefix
		{
			get{return parameterPrefix;}
			set{parameterPrefix = value;}
		}
		
		public Type DbConnectionType {
			get {return dbConnectionType;}
			set {dbConnectionType = value;}
		}
		
		public IDbConnection DbConnection {
			get {
				if (dbConnection == null) {
					//creamos la conexión (algo como new MySqlConnection(connectionString);)
					dbConnection = (IDbConnection)Activator.CreateInstance(dbConnectionType, connectionString);
					dbConnection.Open();
				}
				return dbConnection;
			}
		}
		
		public void Close()
		{
			dbConnection.Close();
		}
	}
}
